/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Web_service_simulation;

import Entity.Contacts;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Parameter;

/**
 *
 * @author sabrina
 */
public abstract class AbstractFacade<T> {

    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    // OK
    public T findCountry(Object country) {
         //javax.persistence.Query query;

         //query = getEntityManager().createQuery("SELECT c FROM Contacts c WHERE c.country = 'Niger'");
        //query.setParameter("country", country);
        //return query.getResultList();
        return getEntityManager().find(entityClass, country);
    }

    public List<T> findCountry2(Object country) {
        javax.persistence.Query query;

        query = getEntityManager().createQuery("SELECT c FROM Contacts c WHERE c.country = :country");
        Parameter p = query.getParameter("country");
        return query.getResultList();
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0] + 1);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public List<T> findNewAndUpdate(String updatedAt) {
        javax.persistence.Query query;
         //Date uneDate = new Date();
        //uneDate.setYear(2013);
        //uneDate.setMonth(6);
        //uneDate.setDate(1);
        System.out.println("findNewAndUpdate" + updatedAt);

        query = getEntityManager().createQuery("SELECT c FROM Contacts c WHERE c.updatedAt > '"+updatedAt+"'");
//         query = getEntityManager().createQuery("SELECT c, b FROM Contacts c INNER JOIN c.bookingsCollection b WHERE c.updatedAt > '"+updatedAt+"'");
        //String queryStr = "SELECT c.booking , b FROM Contacts c, Bookings b WHERE c.updateAt> '" + updatedAt + "' AND b.contacts = c";
        //query = getEntityManager().createQuery(queryStr);

        //query.getParameters("updatedAt");
        return query.getResultList();
    }

    public List<T> findDate(String date) {
        javax.persistence.Query q;
        q = getEntityManager().createNamedQuery("Contacts.findByFirstname");

        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

}
